﻿using MediatR;
using TraceabilityApp_Api.Application.Command.Breed_养殖_;
using TraceabilityApp_Api.DTO;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Breed_养殖_
{
    public class GetBreedHandler : IRequestHandler<GetBreedCommand, ApiResult<BreedDTO>>
    {
        private readonly IBase<BreedModel> breedRepo;

        public GetBreedHandler(IBase<BreedModel> breedRepo)
        {
            this.breedRepo = breedRepo;
        }
        public Task<ApiResult<BreedDTO>> Handle(GetBreedCommand request, CancellationToken cancellationToken)
        {
            ApiResult<BreedDTO> paging = new ApiResult<BreedDTO>();
            var breed = breedRepo.GetAll().Where(x => !x.IsDeleted);
            if(!string.IsNullOrEmpty(request.Batch))
            {
                breed = breed.Where(x=>x.Batch.Contains(request.Batch));
            }
            var linq = (from a in breed
                       select new BreedDTO
                       {
                           BreedId=a.BreedId,
                           Batch=a.Batch,
                           FeedBrand=a.FeedBrand,
                           HarvestDate  =a.HarvestDate,
                           Herdsperson =   a.Herdsperson,
                           HousingDays=a.HousingDays,
                           Source=a.Source,
                           Variety=a.Variety,
                       }).FirstOrDefault(x=>x.Batch==request.Batch);
            paging.Data =linq;
            paging.Code = ApiEnum.OK;
            paging.Msg = "溯源查询列表显示成功";
            return Task.FromResult(paging);
        }
    }
}
